/** 
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 * 
 *QemDutyDeptController
 * 维护-责任主体部门配置
 * 
 * 修改纪录
 * 2023-01-06 版本：1.0 xinfushuo 创建。
 * @version 版本：1.0
 * @author 作者：xinfushuo
 * 创建日期：2023-01-06
 */

package com.topscomm.qem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.qem.pojo.QemDutyDeptEntity;
import org.springframework.web.bind.annotation.*;

import com.topscomm.qem.controller.auto.QemDutyDeptControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: xinfushuo
 * @date: 2023-01-06
 * @description:维护-责任主体部门配置Controller
 * @modify:
 */
@RestController
@RequestMapping("qms/qemDutyDept")
public class QemDutyDeptController extends QemDutyDeptControllerAuto implements IQemDutyDeptController {
	/**
	 * @author: xinfushuo
	 * @date: 2023-01-06
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and enabled = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_depttype")) {
			String value = paraMap.get("qry_depttype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and depttype = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: xinfushuo
	 * @date: 2023-01-06
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: caozhiyuan
	 * @date: 2023-01-09 14:48:24
	 * @param: paraMap
	 * @description: 参照列表SQL拼写
	 * @return: java.lang.String
	 * @modify:
	 */
	@Override
	protected String spellRefListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		// 名称
		this.service.appendSql(paraMap, sql, "qry_name", "and " + QemDutyDeptEntity.FieldName + " like '%{0}%'", value -> value);
		// 编号
		this.service.appendSql(paraMap, sql, "qry_code", "and " + QemDutyDeptEntity.FieldCode + " like '%{0}%'", value -> value);
		// 有效
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and enabled = '{0}' ", value);
		} else {
			// 如果没有参数默认显示有效数据
			sql.appendSingle(" and enabled = 1");
		}
		// 部门类型
		if (paraMap.containsKey("qry_depttype")) {
			String value = paraMap.get("qry_depttype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and depttype = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: caozhiyuan
	 * @date: 2023-01-09 09:14:53
	 * @param: paraMap
	 * @description: dataCodeList
	 * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		Map<String, Object> codeMap = new HashMap<String, Object>();
		List<Map<String, Object>> enabledList = datacodeCache
				.getCodeListMap("CboYesNo", "", "sortcode");
		List<Map<String, Object>> deptTypeList = datacodeCache
				.getCodeListMap("QmsDeptType", "", "sortcode");
		// 将白名单处理成字符串List
		List<String> emailWhiteList = datacodeCache
				.getCodeListMap("QmsEmailWhiteList", "", "sortcode").stream().map(
						item -> ConvertUtil.convertToString(item.get("name"))
				).collect(Collectors.toList());
		codeMap.put("enabledList", enabledList);
		codeMap.put("depttypeList", deptTypeList);
		codeMap.put("emailWhiteList", emailWhiteList);
		super.dataCodeList(paraMap);
		return ResponseResult.ok(codeMap);
	}

	/**
	 * @author: caozhiyuan
	 * @date: 2023-01-10 17:18:24
	 * @param: paraMap
	 * @description:  根据责任主体部门编号获取质量运营专员
	 * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify:
	 */
	@GetMapping(value = "/getUsersByCode")
	public ResponseResult<Object> getUsersByCode(@RequestParam(required = false) Map<String, Object> paraMap) {
		Map<String, Object> data = this.service.getUsersByCode(paraMap);
		return ResponseResult.ok(data);
	}

	/**
	 * @author: caozhiyuan
	 * @date: 2023-01-11 09:18:08
	 * @description: 获取当前用户拥有权限的责任主体部门，以英文逗号分割
	 * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify:
	 */
	@GetMapping(value = "/getAuthorizationByActiveUser")
	public ResponseResult<Object> getAuthorization() {
		List<String> dutyDeptCodes = this.service.getAuthorizationByActiveUser();
		return ResponseResult.ok(dutyDeptCodes);
	}
}

